package cn.itcast.bos.web.action.common;


import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;

import org.apache.struts2.ServletActionContext;
import org.springframework.data.domain.Page;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

import cn.itcast.bos.domain.base.Area;

public class BaseAction<T> extends ActionSupport implements ModelDriven<T>{

	private static final long serialVersionUID = 1L;
	
	protected T model;
	@Override
	public T getModel() {
		return model;
	}
	
	//构造器完成model实例化
	public BaseAction() {
		//构造子类Action对象，获取继承父类型的泛型，然后实例化
		Type genericSuperclass = this.getClass().getGenericSuperclass();
		//获取类型第一个泛型参数
		ParameterizedType parameterizedType = (ParameterizedType) genericSuperclass;
		Class<T> modelClass = (Class<T>) parameterizedType.getActualTypeArguments()[0];
		
		try {
			model = modelClass.newInstance();
		} catch (Exception e) {
			e.printStackTrace();
		}
	
	}
	
	//条件分页查询
		protected Integer page;
		protected Integer rows;
		public void setPage(Integer page) {
			this.page = page;
		}
		public void setRows(Integer rows) {
			this.rows = rows;
		}
		

		protected void pushPageDataToValueStack(Page<T> pageData) {
			Map<String,Object> map = new HashMap<String,Object>();
			map.put("total", pageData.getTotalElements());
			map.put("rows", pageData.getContent());
			ServletActionContext.getContext().getValueStack().push(map);
		}
}
